﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.成品出库单.service;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.Const;
using DXApplicationTangche.UC.门店出入库.UC;
using DXApplicationTangche.UC.成衣进销存.service;
using static DXApplicationTangche.UC.成衣进销存.service.成衣进销存Service;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.report;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.UC.钉钉;
using System.IO;

namespace DXApplicationTangche.UC.成品出库单
{
    public partial class Form抖音出库 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInOutDetailDTO> StockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO stockInOutDetailDTO = new StockInOutDetailDTO();
        private StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO();
        private String sessionId;
        public Form抖音出库()
        {
            InitializeComponent();
            this.sessionId = new FunctionHelper().Uuid;
        }

        private void textBox扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox扫描.Text))
                {
                    try
                    {
                            //扫描成衣码出库
                            StockOutDetailLimitMessageReslt plainMessageResult =
                                成品出库单Service.preStockInOutDetailWithCustomerInfoByBarcodeLimit(
                                    this.sessionId,
                                    this.textBox扫描.Text.Trim(),
                                    0,
                                    StockInOutTypeConst.DouyinStockOut,
                                    ""
                                    );
                            FunctionHelper.JsonSerialization(plainMessageResult.stockInOutDetailDTO);
                            if (!"200".Equals(plainMessageResult.errorCode))
                            {
                                //  最新出入库记录为出库，不能重复出库
                                StockInOutDetailDTO stockInOutDetailDTO = plainMessageResult.stockInOutDetailDTO;
                                MessageBox.Show(stockInOutDetailDTO.toString());
                                this.textBox扫描.Clear();
                                return;
                            }
                            else
                            {
                                //  出库
                                this.stockInOutDetailDTO = plainMessageResult.stockInOutDetailDTO;
                                try { this.stockInOutDetailDTO.styleSizeCd = this.stockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                                if (!String.IsNullOrEmpty(this.stockInOutDetailDTO.barcode))
                                {
                                    if (this.StockInOutDetailDTOs.Count > 0)
                                    {
                                        if (!this.StockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.stockInOutDetailDTO.barcode)))
                                        {
                                            this.StockInOutDetailDTOs.Add(this.stockInOutDetailDTO);
                                        }
                                    }
                                    else
                                    {
                                        this.StockInOutDetailDTOs.Add(this.stockInOutDetailDTO);
                                    }
                                }
                                this.gridControl成品出库.DataSource = this.StockInOutDetailDTOs;
                                this.gridView成品出库.RefreshData();
                                this.gridView成品出库.BestFitColumns();
                            }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.textBox扫描.Clear();
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.StockInOutDetailDTOs.Count > 0)
            {
                try
                {
                    this.出库单DTO =
                        成品出库单Service.getSaveStockInOutDetail(
                            this.sessionId,
                            0,
                            StockInOutTypeConst.DouyinStockOut,
                            "",
                            "", 
                            "8",
                            "退回冻结存货",
                            "fengjinyue01"
                            );
                    if (!String.IsNullOrEmpty("抖音出库"))
                    {
                        this.出库单DTO = 成品出库单Service.saveStockInOutTitleRemarks(this.sessionId, "抖音出库");
                    }
                    DXApplicationTangche.UC.成品出库单.service.出库单导出DTO 出库单导出DTO = new DXApplicationTangche.UC.成品出库单.service.出库单导出DTO(this.出库单DTO, this.StockInOutDetailDTOs);
                    String str = FunctionHelper.JsonSerialization(出库单导出DTO);
                    XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
                    report.抖音();
                    try
                    {
                        //  上传pdf:
                        string saveDirectoryPath = @"pdf\";
                        //  1.先本地生成文件
                        report.ExportToPdf(saveDirectoryPath + this.出库单DTO.inOutCode + @".pdf");
                        //  2.上传本地文件
                        String re = 成品出库单Service.uploadStockInOutFileByCode(this.出库单DTO.inOutCode, saveDirectoryPath + this.出库单DTO.inOutCode + @".pdf");
                        //  3.删除本地文件
                        File.Delete(saveDirectoryPath + re);
                        //  钉钉
                        钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音出库, this.出库单DTO, re).build("17866554337",409162359L));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
                    }
                    //  显示
                    ReportPrintTool tool = new ReportPrintTool(report);
                    tool.ShowPreview();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                this.StockInOutDetailDTOs.Clear();
                this.sessionId = new FunctionHelper().Uuid;
                this.gridControl成品出库.DataSource = this.StockInOutDetailDTOs;
                this.gridView成品出库.RefreshData();
                this.gridView成品出库.BestFitColumns();
            }
            else
            {
                MessageBox.Show("无确认数据");
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void gridView成品出库_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}